import * as api from './api';
import {PAGE_CODE} from './constants';
import {
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi
} from 'Shared/actions/currentUserContext';
import {conventTableCondition} from '../utils';
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk)
            dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else
            dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });
    return permissions;
};
/*==============================查询列表================================= */
export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';

export const getListBegin = () => ({
    type: GET_LIST_BEGIN,
});
export const getListSuccess = (data, condition) => ({
    type: GET_LIST_SUCCESS,
    data,
    condition,
});
export const getListFail = () => ({
    type: GET_LIST_FAIL
});

export const onSubmit = data => () => {
    const addCondition = {
        dealerId: data.dealerId,
        code: data.code,
        name: data.name,
        shortCode: data.shortCode,
        shortName: data.shortName,
        contactName: data.contactName,
        contactPhone: data.contactPhone,
        country: data.country,
        regionId: data.region ? data.region.slice(-1)[0] : null,
        address: data.address,
        fax: data.fax,
        zipCode: data.zipCode,
        email: data.email,
        buildDate: data.buildDate,
        juridicalPerson: data.juridicalPerson,
        juridicalPersonPhone: data.juridicalPersonPhone,
        juridicalPersonEmail: data.juridicalPersonEmail,
        generalManager: data.generalManager,
        generalManagerPhone: data.generalManagerPhone,
        generalManagerEmail: data.generalManagerEmail,
        salesDirector: data.salesDirector,
        salesDirectorPhone: data.salesDirectorPhone,
        salesDirectorEmail: data.salesDirectorEmail,
        serviceDirector: data.serviceDirector,
        serviceDirectorPhone: data.serviceDirectorPhone,
        serviceDirectorEmail: data.serviceDirectorEmail,
        marketDirector: data.marketDirector,
        marketDirectorPhone: data.marketDirectorPhone,
        marketDirectorEmail: data.marketDirectorEmail,
        financialController: data.financialController,
        financialControllerPhone: data.financialControllerPhone,
        financialControllerEmail: data.financialControllerEmail,
        salesInterfacePerson: data.salesInterfacePerson,
        salesInterfacePhone: data.salesInterfacePhone,
        salesInterfaceEmail: data.salesInterfaceEmail,
        serviceInterfacePerson: data.serviceInterfacePerson,
        serviceInterfacePhone: data.serviceInterfacePhone,
        serviceInterfaceEmail: data.serviceInterfaceEmail,
        incorporatedDate: data.incorporatedDate,
        startTime: data.startTime,
        authorisedMethod: data.authorisedMethod,
        authorisedArea: data.authorisedArea,
        arrivedAddress: data.arrivedAddress,
        openDate: data.openDate,
        onlineStatus: data.onlineStatus,
        updatedDate: data.updatedDate,
    };
    const updateCondition = {
        code: data.code,
        name: data.name,
        shortCode: data.shortCode,
        shortName: data.shortName,
        contactName: data.contactName,
        contactPhone: data.contactPhone,
        regionId: data.region ? data.region.slice(-1)[0] : null,
        address: data.address,
        fax: data.fax,
        zipCode: data.zipCode,
        email: data.email,
        buildDate: data.buildDate,
        juridicalPerson: data.juridicalPerson,
        juridicalPersonPhone: data.juridicalPersonPhone,
        juridicalPersonEmail: data.juridicalPersonEmail,
        generalManager: data.generalManager,
        generalManagerPhone: data.generalManagerPhone,
        generalManagerEmail: data.generalManagerEmail,
        salesDirector: data.salesDirector,
        salesDirectorPhone: data.salesDirectorPhone,
        salesDirectorEmail: data.salesDirectorEmail,
        serviceDirector: data.serviceDirector,
        serviceDirectorPhone: data.serviceDirectorPhone,
        serviceDirectorEmail: data.serviceDirectorEmail,
        marketDirector: data.marketDirector,
        marketDirectorPhone: data.marketDirectorPhone,
        marketDirectorEmail: data.marketDirectorEmail,
        financialController: data.financialController,
        financialControllerPhone: data.financialControllerPhone,
        financialControllerEmail: data.financialControllerEmail,
        salesInterfacePerson: data.salesInterfacePerson,
        salesInterfacePhone: data.salesInterfacePhone,
        salesInterfaceEmail: data.salesInterfaceEmail,
        serviceInterfacePerson: data.serviceInterfacePerson,
        serviceInterfacePhone: data.serviceInterfacePhone,
        serviceInterfaceEmail: data.serviceInterfaceEmail,
        incorporatedDate: data.incorporatedDate,
        startTime: data.startTime,
        authorisedMethod: data.authorisedMethod,
        authorisedArea: data.authorisedArea,
        arrivedAddress: data.arrivedAddress,
        openDate: data.openDate,
        onlineStatus: data.onlineStatus,
        updatedDate: data.updatedDate,
        rowVersion: data.rowVersion,
    };
    return data.id ? api.modifySubDealer(data.id, updateCondition)
        .then(res => ({success: res.ok})) : api.newSubDealer(addCondition)
        .then(res => ({success: res.ok}));
};

/*==============================查询action================================= */
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});

/*==============================重置查询条件================================= */
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL
});

export const getList = condition => dispatch => {
    if(!condition)
        return;
    dispatch(getListBegin());
    const queryCondition = conventTableCondition(condition);
    return api.getList(queryCondition).then(result => {
        if(result.ok)
            dispatch(getListSuccess(result.data, condition));
        else
            dispatch(getListFail());
    });
};

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};

//分页排序搜索
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

//修改维修项目查询的条件
export const SAVE_DEALER_CONDITION = 'SAVE_DEALER_CONDITION';
export const saveDealerCondition = (name, value) => ({
    type: SAVE_DEALER_CONDITION,
    name,
    value
});
